package net.jquant.downloader;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import com.google.common.base.Joiner;
import com.google.common.collect.Maps;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * author: eryk mail: xuqi86@gmail.com date: 15-8-15.
 */
public class BasicDownloader {
    private static final Logger LOG = LoggerFactory.getLogger(BasicDownloader.class);

    private static int RETRY_COUNT = 1;

    public static String download(final String url, final Map<String, String> header) {
        int retry = 0;
        while (retry <= RETRY_COUNT) {
            try {
                HttpResponse<String> response = null;
                if (header.size() > 0) {
                    response = Unirest.get(url).headers(header).asString();
                } else {
                    response = Unirest.get(url).asString();
                }
                if (response.getStatus() == 200) {
                    return response.getBody();
                } else {
                    LOG.warn("status=" + response.getStatus() + ",url=" + url);
                }
                retry++;
            } catch (final UnirestException e) {
                retry++;
                LOG.error("retry=" + retry + ",fail to download from " + url);
                try {
                    Thread.sleep(1000);
                } catch (final InterruptedException e1) {
                    LOG.error("fail to download from" + url);
                }
            }
        }
        return "";
    }

    public static String download(final String url) {
        return download(url, Maps.newHashMap());
    }

    public static InputStream downloadStream(final String url) {
        int retry = 0;
        while (retry <= RETRY_COUNT) {
            try {
                final HttpResponse<String> response = Unirest.get(url).asString();
                if (response.getStatus() == 200) {
                    return response.getRawBody();
                } else {
                    LOG.warn("status=" + response.getStatus() + ",url=" + url);
                }
                retry++;
            } catch (final UnirestException e) {
                retry++;
                LOG.error("fail to download from " + url);
            }
        }
        return null;
    }

    public static String download(final String url, final String encoding) {
        final InputStream inputStream = downloadStream(url);
        if (inputStream != null) {
            try {
                final List<String> strings = IOUtils.readLines(inputStream, encoding);
                return Joiner.on("\n").join(strings);
            } catch (final IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }

}
